package com.kkd.kkdapp.view;

import java.text.DecimalFormat;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;

import com.kkd.kkdapp.utils.SystemUtils;

public class XianduanView extends View {

	private Paint paint;
	private int screenWidth;// 屏幕宽度
	private int lll;// 长线的高度
	private int mmm;// 中线的高度
	private int sss;// 短线的高度
	private int width;// 每段的宽度
	private float percent = 1f;// 每段的百分比
	private int count; // 一共画的段数
	private DecimalFormat df = new DecimalFormat("###");

	public XianduanView(Context context, AttributeSet attrs) {
		super(context, attrs);
		lll = SystemUtils.dip2px(getContext(), 15);
		mmm = SystemUtils.dip2px(getContext(), 10);
		sss = SystemUtils.dip2px(getContext(), 5);
		screenWidth = getScreenWidth(getContext());
		count = (int) (10 / percent);
		int num = (int) (5 / percent + 1);// 一半屏幕画的段数，用于计算每段的宽度
		width = screenWidth / 2 / num;
		paint = new Paint();
		paint.setColor(Color.parseColor("#cccccc"));
		paint.setAntiAlias(true);
		paint.setTextSize(sp(12));
		paint.setFlags(Paint.ANTI_ALIAS_FLAG);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2, paint);
		// canvas.drawLine(screenWidth / 2, getHeight() / 2, getWidth()
		// - screenWidth / 2, getHeight() / 2, paint);
		for (int i = 0; i <= count; i++) {
			if (i % ((int) (5 / percent)) == 0) {
				canvas.drawLine(screenWidth / 2 + i * width, getHeight() / 2,
						screenWidth / 2 + i * width, getHeight() / 2 - lll,
						paint);
				String flag = df.format(90 + i * percent) + "%";
				canvas.drawText(flag, screenWidth / 2 + i * width
						- getStringWidth(flag) / 2, getHeight() / 2
						+ getFontHeight(), paint);
			} else {
				if (i % 2 == 0) {
					canvas.drawLine(screenWidth / 2 + i * width,
							getHeight() / 2, screenWidth / 2 + i * width,
							getHeight() / 2 - mmm, paint);
				} else {
					canvas.drawLine(screenWidth / 2 + i * width,
							getHeight() / 2, screenWidth / 2 + i * width,
							getHeight() / 2 - sss, paint);
				}
			}
		}
		// 画左边多余部分
		int num = (int) (5 / percent + 1);// 一半屏幕画的段数，用于计算每段的宽度
		for (int i = 0; i < num; i++) {
			if (i % ((int) (5 / percent)) == 0) {
				canvas.drawLine(screenWidth / 2 - i * width, getHeight() / 2,
						screenWidth / 2 - i * width, getHeight() / 2 - lll,
						paint);
				// String flag = df.format(90 - i * percent) + "%";
				// canvas.drawText(flag, screenWidth / 2 - i * width
				// - getStringWidth(flag) / 2, getHeight() / 2
				// + getFontHeight(), paint);
			} else {
				if (i % 2 == 0) {
					canvas.drawLine(screenWidth / 2 - i * width,
							getHeight() / 2, screenWidth / 2 - i * width,
							getHeight() / 2 - mmm, paint);
				} else {
					canvas.drawLine(screenWidth / 2 - i * width,
							getHeight() / 2, screenWidth / 2 - i * width,
							getHeight() / 2 - sss, paint);
				}
			}
		}
		// 画右边多余部分
		for (int i = 0; i < num; i++) {
			if (i % ((int) (5 / percent)) == 0) {
				canvas.drawLine(getWidth() - screenWidth / 2 + i * width,
						getHeight() / 2, getWidth() - screenWidth / 2 + i
								* width, getHeight() / 2 - lll, paint);
				// String flag = df.format(100 + i * percent) + "%";
				// canvas.drawText(flag, getWidth() - screenWidth / 2 + i *
				// width
				// - getStringWidth(flag) / 2, getHeight() / 2
				// + getFontHeight(), paint);
			} else {
				if (i % 2 == 0) {
					canvas.drawLine(getWidth() - screenWidth / 2 + i * width,
							getHeight() / 2, getWidth() - screenWidth / 2 + i
									* width, getHeight() / 2 - mmm, paint);
				} else {
					canvas.drawLine(getWidth() - screenWidth / 2 + i * width,
							getHeight() / 2, getWidth() - screenWidth / 2 + i
									* width, getHeight() / 2 - sss, paint);
				}
			}
		}
	}

	// 获取字体串宽度
	private int getStringWidth(String str) {
		return (int) paint.measureText(str);
	}

	// 获取字体高度
	private int getFontHeight() {
		FontMetrics fm = paint.getFontMetrics();
		return (int) Math.ceil(fm.descent - fm.top) + 2;
	}

	public int getShouldWidth() {
		return count * width + screenWidth;
	}

	public int get100() {
		return count * width;
	}

	// 获取屏幕宽度
	public static int getScreenWidth(Context context) {
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		int width = wm.getDefaultDisplay().getWidth();
		return width;
	}

	private float sp(final int sp) {
		float scaledDensity = getResources().getDisplayMetrics().scaledDensity;
		return sp * scaledDensity;
	}

	public void setShouldWidth() {
		LayoutParams params = getLayoutParams();
		params.width = getShouldWidth();
		setLayoutParams(params);
	}

	public float getMid(int scrollX) {
		float itemNum = 1f * scrollX / width;
		float cur_percent = itemNum * percent + 90;
		return cur_percent;
	}

	/**
	 * 设置每段的百分比
	 * 
	 * @param percent
	 */

	public void setPercent(float percent) {
		try {
			this.percent = percent;
			count = (int) (10 / percent);
			int num = (int) (5 / percent + 1);// 一半屏幕画的段数，用于计算每段的宽度
			width = screenWidth / 2 / num;
			setShouldWidth();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
